package com.letoken.platform.repo.app.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.letoken.platform.repo.app.dao.AppUserSessionMapper;
import com.letoken.platform.repo.app.po.AppUserSession;
import com.letoken.platform.repo.app.redis.AppLoginSessionRedis;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @version 1.0
 * @author： Christopher
 * @date： 2022/1/8 17:12
 */
@Log4j2
@Repository
public class AppUserSessionManager {


    @Autowired
    private AppUserSessionMapper appUserSessionMapper;


    /**
     * 根据id和设备id查询session
     *
     * @param id
     * @return
     */
    public AppUserSession queryAppUserSessionById(Integer id) {
        AppUserSession appUserSession = AppLoginSessionRedis.me().getClassObject(String.valueOf(id));
        if (appUserSession == null){
            QueryWrapper<AppUserSession> query = new QueryWrapper<>();
            query.eq("id", id);
            return appUserSessionMapper.selectOne(query);
        }
       return appUserSession;
    }


    /**
     * 添加session
     *
     * @param appUserSession
     * @return
     */
    public Integer addSession(AppUserSession appUserSession) {
        int i = appUserSessionMapper.insert(appUserSession);
        if (0 < i) {
            AppLoginSessionRedis.me().put(String.valueOf(appUserSession.getId()), appUserSession);
        }
        return i;
    }


    /**
     * 修改session
     *
     * @param appUserSession
     * @return
     */
    public Integer updateByIdAndDeviceId(AppUserSession appUserSession) {
        QueryWrapper<AppUserSession> query = new QueryWrapper<>();
        query.eq("id", appUserSession.getId())
                .eq("device_id", appUserSession.getDeviceId());
        int i = appUserSessionMapper.update(appUserSession, query);
        if (0 < i) {
            AppLoginSessionRedis.me().put(String.valueOf(appUserSession.getId()), appUserSession);
        }
        return i;
    }

//    /**
//     * 查询所有的支持推送的设备id集合
//     *
//     * @return
//     */
//    public List<AppUserSession> queryDeviceTokenList() {
//        return appUserSessionMapper.queryDeviceTokenList();
//    }



}
